<?php
/**
 * 手机端首页控制
 *
 *by wansyb QQ群：111731672
 *你正在使用的是由网店 运 维提供S2.0系统！保障你的网络安全！ 购买授权请前往shopnc
 */



defined('ByShopWWI') or exit('Access Invalid!');
class indexControl extends mobileHomeControl{

    public function __construct() {
        parent::__construct();
    } 

    /**
     * 首页
     */
    public function indexOp() {
        $model_mb_special = Model('mb_special');
        $data = $model_mb_special->getMbSpecialIndex();
		
        $this->_output_special($data, $_GET['type']);
    }

    /**
     * 专题
     */
    public function specialOp() {
        $model_mb_special = Model('mb_special');
        $data = $model_mb_special->getMbSpecialItemUsableListByID($_GET['special_id']);
		$datas['special_desc'] = $model_mb_special->getMbSpecialdesc($_GET['special_id']);
        $this->_output_special($data, $_GET['type'], $_GET['special_id'],$datas);
    }

    /**
     * 输出专题
     */
    private function _output_special($data, $type = 'json', $special_id = 0, $datas=array()) {
        $model_special = Model('mb_special');
		
        if($_GET['type'] == 'html') {
            $html_path = $model_special->getMbSpecialHtmlPath($special_id);
            if(!is_file($html_path)) {
                ob_start();
                Tpl::output('list', $data);
                Tpl::showpage('mb_special');
                file_put_contents($html_path, ob_get_clean());
            }
            header('Location: ' . $model_special->getMbSpecialHtmlUrl($special_id));
            die;
        } else {
		$datas['list'] = $data;
            $datas['special_id'] = $special_id;
            $datas = $datas;
            output_data($datas);
        }
    }
    /**
     * 默认搜索词列表
     */
    public function search_key_listOp() {
		//热门搜索
        $list = @explode(',',C('hot_search'));
        if (!$list || !is_array($list)) { 
            $list = array();
        }
                
        //历史搜索
        if (cookie('his_sh') != '') {
            $his_search_list = explode('~', cookie('his_sh'));
        }

        $data['list'] = $list;
		$data['his_list'] = is_array($his_search_list) ? $his_search_list : array();
		output_data($data);
    }
	
    /**
     * 热门搜索列表
     */
    public function search_hot_infoOp() {
				//热门搜索
        if (C('rec_search') != '') {
            $rec_search_list = @unserialize(C('rec_search'));
			$rec_value = array();
			$i=0;
			foreach($rec_search_list as $v){
				$rec_value[] = $v['value'];
				$i++;
				if($i>7){
					break;
				}
			}
			
        }
        output_data(array('hot_info'=>$result ? $rec_value : array()));
    }
	//轮播图
	function img_indexOp(){
		$model_mb_special = Model('mb_special');
        $data = $model_mb_special->getMbSpecialIndex();
		foreach($data[0]['adv_list'] as $key=>$row){
			foreach($row as $k=>$rows){
			if($rows['type']=='url'){
				if(strpos($rows['data'],'goods_id')){
					$data[0]['adv_list']['item'][$k]['id']=strstr($rows['data'],'=');
					$data[0]['adv_list']['item'][$k]['urltype']='goods';
				}elseif(strpos($rows['data'],'store_id')){
					$data[0]['adv_list']['item'][$k]['id']=strstr($rows['data'],'=');
					$data[0]['adv_list']['item'][$k]['urltype']='store';
				}elseif(strpos($rows['data'],'gc_id')){
					$data[0]['adv_list']['item'][$k]['id']=strstr($rows['data'],'=');
					$data[0]['adv_list']['item'][$k]['urltype']='goods_class';
				}else{
					$data[0]['adv_list']['item'][$k]['id']='';
					$data[0]['adv_list']['item'][$k]['urltype']='index';
				}
			}else{
				$data[0]['adv_list']['item'][$k]['id']='';
					$data[0]['adv_list']['item'][$k]['urltype']='index';
			}
			}
		}
		output_data($data[0]['adv_list']);
		
	} 
//推荐商品
	function rec_goodsOp(){
		$model_mb_special = Model('mb_special');
        $data = $model_mb_special->getMbSpecialIndex();
		foreach($data as $key=>$lis){
			if($lis['goods']){
				$goods_list[] = $lis['goods'];
			}
		}
		$field = 'goods_salenum,goods_marketprice';
		$model_goods =Model('goods');
		$goods_lists =array();
	
		$goods_lists =$goods_list;
		foreach($goods_list as $keys=>$list){
			foreach($list['item'] as $key=>$row){
				$contion['goods_id'] = $row['goods_id'];
				$count  = $model_goods->getGoodsList($contion,$field);
				$goods_lists[$keys]['item'][$key]['goods_salenum'] =$count[0]['goods_salenum'];	
				//$goods_lists[$keys]['item'][$key]['goods_salenum'] = $count[0];
				$goods_lists[$keys]['item'][$key]['goods_marketprice'] = $count[0]['goods_marketprice'];
			}
		} 
		output_data($goods_lists);	
	}	

    /**
     * 高级搜索
     */
    public function search_advOp() {
        $area_list = Model('area')->getAreaList(array('area_deep'=>1),'area_id,area_name');
        if (C('contract_allow') == 1) {
            $contract_list = Model('contract')->getContractItemByCache();
            $_tmp = array();$i = 0;
            foreach ($contract_list as $k => $v) {
                $_tmp[$i]['id'] = $v['cti_id'];
                $_tmp[$i]['name'] = $v['cti_name'];
                $i++;
            }
        }
        output_data(array('area_list'=>$area_list ? $area_list : array(),'contract_list'=>$_tmp));
    }

    /**
     * android客户端版本号
     */
    public function apk_versionOp() {
        $version = C('mobile_apk_version');
        $url = C('mobile_apk');
        if(empty($version)) {
           $version = '';
        }
        if(empty($url)) {
            $url = '';
        }

        output_data(array('version' => $version, 'url' => $url));
    }
	
	//系统数据统计
	public function count_numberOp(){

		$model_point = Model('points');
		//新增积分 分红豆turncash  redpoints 宝豆turnpoints

		$nowday=date("Y-m-d 23:59:59",strtotime("-1 day"));
		$ytoday=date("Y-m-d",strtotime("-1 day"));
		$today=date("Y-m-d");
		$yestoday=date("Y-m-d",strtotime("-1 day"));
		$nowday=strtotime($nowday);
		$ytoday=strtotime($ytoday);
		//上一天升级积分
		$cont=array();
		$fiel='';
		$fiel='sum(pl_points) as ypoints';
		$cont['pl_stage']='upgradepoints';
		$cont['saddtime']=$ytoday;
		$cont['eaddtime']=$nowday;
		$row_up= $model_point->getPointsLogList($cont,'',$fiel);
		
		//门店消费豆storepoints
		$cont=array();
		$fiel='';
		$fiel='sum(pl_points) as ypoints';
		$cont['pl_stage']='storepoints';
		$cont['saddtime']=$ytoday;
		$cont['eaddtime']=$nowday;
		$row_st= $model_point->getPointsLogList($cont,'',$fiel);
		
		
		//在线订购的积分
		$cont=array();
		$fiel='';
		$fiel='sum(pl_points) as ypoints';
		$cont['pl_stage']='order';
		$cont['saddtime']=$ytoday;
		$cont['eaddtime']=$nowday;
		$row_s= $model_point->getPointsLogList($cont,'',$fiel);
		
		
		//登录赠送
		$cont=array();
		$fiel='';
		$fiel='sum(pl_points) as ypoints';
		$cont['pl_stage']='login';
		$cont['saddtime']=$ytoday;
		$cont['eaddtime']=$nowday;
		$row_lo= $model_point->getPointsLogList($cont,'',$fiel);
		
		//注册赠送
		$cont=array();
		$fiel='';
		$fiel='sum(pl_points) as ypoints';
		$cont['pl_stage']='regist';
		$cont['saddtime']=$ytoday;
		$cont['eaddtime']=$nowday;
		$row_re= $model_point->getPointsLogList($cont,'',$fiel);
		//上一天积分总和
		$allpoints=$row_up[0]['ypoints']+$row_st[0]['ypoints']+$row_s[0]['ypoints']+$row_lo[0]['ypoints']+$row_re[0]['ypoints'];
		$data['allpoints']=abs($allpoints);
		$cont=array();
		$cont=array();
		$fiel='';
		$fiel='sum(pl_points) as rpoints';
		$cont['pl_stage']='turnpoints';
		$cont['saddtime']=$ytoday;
		$cont['eaddtime']=$nowday;
		$row= $model_point->getPointsLogList($cont,'',$fiel);
		
		$rpoints = $row[0]['rpoints'];
		
		if($rpoints==""){
			$rpoints=0;
		}
		//一天返还宝豆总量
		$data['rpoints']=abs($rpoints);
		$sql="select * from shopwwi_returnpoint order by id desc";
		$res_sr=mysql_query($sql);
		$row_sr=mysql_fetch_array($res_sr);
		$back_point=$row_sr['back_point'];
		if($back_point==""){
			$back_point=0;
		} 
		$avpoints = $rpoints/$back_point;//上一天全返单元总量
		$data['avpoints']=abs($avpoints);
		$data['back_point']=abs($back_point);//上一天每单元返还积分数
		output_data($data);	
	}
	public function member_slideOp(){
		 $adv  = Model('adv');
		 $condition['ap_id']=39;
		$ap_list = $adv->getList($condition);
		foreach($ap_list as $k=>$row){
			$ap_list[$k]['adv_content']= unserialize($row['adv_content']);
			$ap_list[$k]['adv_content']['adv_pic']= UPLOAD_SITE_URL."/data/upload/shop/adv/".$ap_list[$k]['adv_content']['adv_pic'];
			
			if(strpos($ap_list[$k]['adv_content']['adv_pic_url'],'goods_id')){
					$ap_list[$k]['id']=strstr($ap_list[$k]['adv_content']['adv_pic_url'],'=');
					$ap_list[$k]['urltype']='goods';
				}elseif(strpos($ap_list[$k]['adv_content']['adv_pic_url'],'store_id')){
					$ap_list[$k]['id']=strstr($ap_list[$k]['adv_content']['adv_pic_url'],'=');
					$ap_list[$k]['urltype']='store';
				}elseif(strpos($ap_list[$k]['adv_content']['adv_pic_url'],'gc_id')){
					$ap_list[$k]['id']=strstr($ap_list[$k]['adv_content']['adv_pic_url'],'=');
					$ap_list[$k]['urltype']='goods_class';
				}else{
					$ap_list[$k]['id']='';
					$ap_list[$k]['urltype']='index';
			}
			
		}
		output_data($ap_list);
	}
}
